// pages/lawyerTool/lawyerTool.js
import postData from '../../../utils/vendor/common/common.js';
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isCaseShow: false,
    suitFeePlan:false,
    targetAmount:"",//标的金额
    adjustmentFee:"",//受理费
    securityFee: "",//保全费
    executionFee: "",//执行费
    animationData1: {},
    animationData2:{},
    conditionData: {
      caseType:"财产案件"
    },
    suitTypeList: "",//案件类型列表
    suitFeeData:{},//案件类型对象
    contentDet: postData.suitFeePlan[0].content,
    hanziNum:'',
    showClear: false,
    showBtn:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log("options=======", options)
    if (options.id == 'share'){
      this.setData({
        showBtn: true
      });
    }
    let suitFeeData = {};
    let suitTypeList = postData.suitTypeList;

    let tempCaseList = postData.suitFeeJson;
    for (let i = 0; i < tempCaseList.length; i++) {
      suitFeeData[tempCaseList[i].type] = tempCaseList[i];
    }

    let typeData = suitFeeData[this.data.conditionData.caseType];
    let adjustmentFee = typeData.adjustmentFee(this.data.targetAmount);
    let securityFee = typeData.securityFee(this.data.targetAmount);
    let executionFee = typeData.executionFee(this.data.targetAmount);

    this.setData({
      contentDet: postData.convertHtmlToText(this.data.contentDet),
      suitTypeList: suitTypeList,
      suitFeeData: suitFeeData,
      adjustmentFee: adjustmentFee,
      securityFee: securityFee,
      executionFee: executionFee
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    // this.tool = this.selectComponent("#tool");
  },

  //判断是否位数字
  judeNum: function(value){
    var patrn = /^(-)?\d+(\.\d+)?$/;
    if (patrn.exec(value) == null || value == "") {
      return false
    } else {
      return true
    }
  },


  //触发输入金额
  targetAmountChange: function(e) {
    // console.log(e.detail.value);
    let value = e.detail.value;
    let suitFeeData = this.data.suitFeeData;
    let typeData = suitFeeData[this.data.conditionData.caseType];
    
    if (!this.judeNum(value)){
      let adjustmentFee = typeData.adjustmentFee("");
      let securityFee = typeData.securityFee("");
      let executionFee = typeData.executionFee("");
      this.setData({
        targetAmount: "",
        showClear: false,
        hanziNum:"",
        adjustmentFee: adjustmentFee,
        securityFee: securityFee,
        executionFee: executionFee
      });
    }else{
      let hanziNum = postData.numToHanzi(e.detail.value);
      let adjustmentFee = typeData.adjustmentFee(value);
      let securityFee = typeData.securityFee(value);
      let executionFee = typeData.executionFee(value);
      this.setData({
        targetAmount: value,
        showClear: true,
        hanziNum: hanziNum,
        adjustmentFee: adjustmentFee,
        securityFee: securityFee,
        executionFee: executionFee
      });
    }
  },

  //清空标的额
  clearNum: function() {
    let suitFeeData = this.data.suitFeeData;
    let typeData = suitFeeData[this.data.conditionData.caseType];
    let adjustmentFee = typeData.adjustmentFee("");
    let securityFee = typeData.securityFee("");
    let executionFee = typeData.executionFee("");
    this.setData({
      targetAmount: "",
      showClear: false,
      hanziNum: "",
      adjustmentFee: adjustmentFee,
      securityFee: securityFee,
      executionFee: executionFee
    })
  },

  // 点击显示案件类型
  showCaseTypeList : function(){
    this.setData({
      isCaseShow: true
    })
    var animation = wx.createAnimation({
      duration: 300,
      timingFunction: 'ease-out',
    })
    this.animation = animation
    animation.translateY(0).step()
    this.setData({
      animationData1: animation.export()
    })
  },


  //获取案件类型的值
  cancleTypeListlist: function(e) {
    let animation = wx.createAnimation({
      duration: 300,
      timingFunction: 'ease-out',
    })
    this.animation = animation
    animation.translateY('100%').step()
    this.setData({
      animationData1: animation.export()
    })

    let key = e.currentTarget.dataset.key;
    let suitTypeList = this.data.suitTypeList;
    let suitFeeData = this.data.suitFeeData;
    // console.log('e=====',e)
    // console.log("key=====",key)
    // console.log("suitTypeList[key]===", suitTypeList[key])
    // console.log("suitFeeData[suitTypeList[key]]====", suitFeeData[suitTypeList[key]])
    let typeData = suitFeeData[suitTypeList[key]];
    let adjustmentFee = typeData.adjustmentFee(this.data.targetAmount);
    let securityFee = typeData.securityFee(this.data.targetAmount);
    let executionFee = typeData.executionFee(this.data.targetAmount);
    setTimeout(function () {
      this.setData({
        isCaseShow: false,
        conditionData: {
          caseType: suitTypeList[key]
        },
        adjustmentFee: adjustmentFee,
        securityFee: securityFee,
        executionFee: executionFee
      })
    }.bind(this), 400)
    

  },
  // 点击显示诉讼费用交纳办法
  showSuitFeePlan: function() {
    app.globalData.toolContentDet = this.data.contentDet;
    app.globalData.toolTitle = "诉讼费用交纳方法";
    wx.navigateTo({
      url: '../intro/intro',
    }) 
  },

  // 点击去诉讼费
  // _goSSF: function () {
    
  // },

  //点击去律师费
  // _goLSF: function () {
  //   wx.navigateTo({
  //     url: '../lsf/lsf',
  //   })
  // },

  //点击债权计算
  // _goZQJS: function(){
  //   wx.navigateTo({
  //     url: '../zqjs/zqjs',
  //   })
  // },

  //点击回首页
  goHome: function(){
    wx.redirectTo({
      url: '../../index/index',
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
  * 用户点击右上角分享
  */
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log("来自页面内的转发", res.target)
    }
    return {
      title: '律师诉讼费计算工具',
      path: 'pages/lawyerTool/ssf/ssf?id=share',
      // imageUrl: picUrl,
      success: function (res) {
        // 转发成功
        console.log("转发成功返回数据=", res)
        wx.showShareMenu({
          // 要求小程序返回分享目标信息
          withShareTicket: true
        });
      },
      fail: function (res) {
        // 转发失败
      },
      complete: function () {

      }
    }
  }
})